package com.itheima.controller;

import com.itheima.anno.LogOperation;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result findAll(){
        return Result.success(deptService.findAll());
    }

    //删除方式一：使用HttpServletRequest 获取请求参数
    /*@DeleteMapping("/depts")
    public Result deleteById(HttpServletRequest request){
        String id = request.getParameter("id");
        int i = Integer.parseInt(id);
        return Result.success(deptService.deleteById(i));
    }*/
    //删除方式二：使用@RequestParam 获取请求参数
    /*@DeleteMapping("/depts")
    public Result deleteById(@RequestParam("id") Integer id){
        return Result.success(deptService.deleteById(id));
    }*/
    //删除方式三：省略@RequestParam （前提是方法参数名和前端请求参数名保持一致）
    @LogOperation
    @DeleteMapping
    public Result deleteById(Integer id){
        deptService.deleteById(id);
        return Result.success();
    }

    @LogOperation
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("添加部门信息，添加的部门：{}",dept);
        return Result.success(deptService.add(dept));
    }

    @GetMapping("/{id}")
    //public Dept selectById(@PathVariable("id") Integer id){
    public Result selectById(@PathVariable Integer id){
       return Result.success(deptService.selectById(id));
    }

    @LogOperation
    @PutMapping
    public Result updateById(@RequestBody Dept dept){
        return Result.success(deptService.updateById(dept));
    }
}
